package ysq.xyg.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.model.WebCatalog;
import ysq.xyg.model.info.WebCatalogInfo;
import ysq.xyg.model.info.Page;
import ysq.xyg.model.info.ReturnInfo;
import ysq.xyg.service.WebCatalogService;
import ysq.xyg.util.BaseConstant;
import ysq.xyg.util.BaseUtil;
import ysq.xyg.util.DateUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.Date;


@Controller
@RequestMapping("/webCatalog")
public class WebCatalogController extends BaseController {

    private static Logger log= LoggerFactory.getLogger(WebCatalogController.class);
    @Resource
    private WebCatalogService webCatalogService;

    private WebCatalog webCatalog;

    @RequestMapping("/listPage")
    public ModelAndView List(HttpServletRequest request, HttpServletResponse response,String menuid){
        return new ModelAndView("/webCatalogList");
    }

    @RequestMapping(value = "/jsonList",method = RequestMethod.POST)
    public @ResponseBody JSONObject jsonList(WebCatalog webCatalog, Page page) throws MalformedURLException, UnsupportedEncodingException {
        PageInfo<WebCatalog> pageInfo = webCatalogService.page(webCatalog, page.getLimit(), page.getPage());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("count", pageInfo.getTotal());
        jsonObject.put("data",pageInfo.getList());
        return jsonObject;
    }

    @RequestMapping(value = "/selectEntity",method = RequestMethod.GET)
    public ModelAndView selectEntity(String id){
        ModelAndView view = new ModelAndView("/webCatalogUpdateOrSave");
        webCatalog = webCatalogService.selectByPrimaryKey(id);
        view.addObject("webCatalog", webCatalog);
        return view;
    }

    @RequestMapping(value = "/updateOrAddEntity",method = RequestMethod.POST)
    public @ResponseBody JSONObject updateOrAddEntity(WebCatalogInfo webCatalogInfo) {
        ReturnInfo returnInfo = new ReturnInfo();
        JSONObject jsonObject = new JSONObject();
        if (StringUtil.isEmpty(webCatalogInfo.getId())) {
            webCatalog = new WebCatalog();
            webCatalog.setId(BaseUtil.uId());
            webCatalog.setType(BaseConstant.BANNER_IS_BANNER);
            webCatalog.setIconPic(webCatalogInfo.getIconPic());
            webCatalog.setShowPic(webCatalogInfo.getShowPic());
            webCatalog.setDescription(webCatalogInfo.getDescription());
            webCatalog.setName(webCatalogInfo.getName());
            webCatalog.setSort(webCatalogInfo.getSort());
            webCatalog.setCreateTime(new Date());
            int id = webCatalogService.insert(webCatalog);
            if(id <= 0){
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_INSERT);
            }
        } else {
            webCatalog = webCatalogService.selectByPrimaryKey(webCatalogInfo.getId());
            webCatalog.setType(BaseConstant.BANNER_IS_BANNER);
            webCatalog.setSort(webCatalogInfo.getSort());
            webCatalog.setShowPic(webCatalogInfo.getShowPic());
            webCatalog.setIconPic(webCatalogInfo.getIconPic());
            webCatalog.setDescription(webCatalogInfo.getDescription());
            webCatalog.setName(webCatalogInfo.getName());
            int id = webCatalogService.updateByPrimaryKey(webCatalog);
            if (id <= 0) {
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
        jsonObject.put("returnInfo", returnInfo);
        return jsonObject;
    }

    @RequestMapping(value = "/deleteEntity",method = RequestMethod.GET)
    public @ResponseBody JSONObject deleteEntity(String id){
        ReturnInfo returnInfo=new ReturnInfo();
        JSONObject jsonObject=new JSONObject();
        if(StringUtil.isEmpty(id)){
            returnInfo=new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_CANSHU);
        }else{
            int ids = webCatalogService.deleteByPrimaryKey(id);
            if(ids <= 0){
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_DELETE);
            }
        }
        jsonObject.put("returnInfo",returnInfo);
        return jsonObject;
    }
}
